import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author wangj881
 * @date 2025-03-04 17:47
 */
public class L104_3 {

    public static void main(String[] args) {
        TreeNode treeNode15 = new TreeNode(15);
        TreeNode treeNode7 = new TreeNode(7);
        TreeNode treeNode20 = new TreeNode(20, treeNode15, treeNode7);
        TreeNode treeNode9 = new TreeNode(9);
        TreeNode treeNode3 = new TreeNode(3, treeNode9, treeNode20);
        new Solution().maxDepth(treeNode3);
    }


    static class Solution {
        public int maxDepth(TreeNode root) {
            LinkedList<TreeNode> nodes = new LinkedList<>();
            int depth = 0;
            if (null == root) {
                return depth;
            }
            nodes.addFirst(root);
            int size = nodes.size();
            while (!nodes.isEmpty()) {
                depth++;
                for (int i = 0; i < size; i++) {
                    TreeNode treeNode = nodes.removeLast();
                    if (null != treeNode.left) {
                        nodes.addFirst(treeNode.left);
                    }
                    if (null != treeNode.right) {
                        nodes.addFirst(treeNode.right);
                    }
                }
                size = nodes.size();
            }
            return depth;
        }
    }
}
